from flask_login import login_required, LoginManager, current_user
from flask_restful import Resource

from app import app
from commons.models import Player, UserToken
from commons.rs_code import rscode_response, RsCode

login = LoginManager(app)


@login.request_loader
@login.user_loader
def auth_request(request):
    """
    登录鉴权的回调函数
    """
    token = request.headers.get('Auth', '')
    # 没有传token
    if not token:
        return

    user = UserToken.get_player(token)

    return user


@login.unauthorized_handler
def unauthorized():
    # do stuff
    return rscode_response(RsCode.NO_LOGIN)
